/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJApplet.java
 *
 * Created on Aug 18, 2009, 11:10:57 PM
 */
package ruteodosciudades;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Insets;
import ruteodosciudades.algoritmo.Dijkstra;

/**
 *
 * @author markos
 */
public class GraphApplet extends javax.swing.JApplet {

    @Override
    public void init() {
        dataBase = "";
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {

                public void run() {
                    initComponents();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        setName("Ruteo entre dos ciudades");
        nodoInicio = getParameter("inicio");
        nodoMeta = getParameter("fin");
         dataBase = getParameter("database");
        if (dataBase.equals("")) {
            dataBase = "ruteo";
        }
//        nodoInicio = "D";
//        nodoMeta = "C";

        dijkstra = new Dijkstra(nodoInicio, nodoMeta, dataBase);

        // se ejecuta el algoritmo
        tiempo = System.currentTimeMillis();
        dijkstra.doDijkstra();
        tiempo = System.currentTimeMillis() - tiempo;
        dijkstra.printPath(nodoMeta);
        setLayout(new BorderLayout(10, 10));
        setPreferredSize(new Dimension(800, 600));

        // agregamos el canvas al applet
        grafocanvas = new GraphCanvas(dataBase);
        grafocanvas.setTiempo(tiempo);
        grafocanvas.setNodos(dijkstra.getNodeMap(), nodoInicio, nodoMeta);
        add(BorderLayout.CENTER, grafocanvas);
        grafocanvas.doGrafo();
    }

    /** This method is called from within the init() method to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setName("Form"); // NOI18N
        setStub(null);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 893, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 421, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    private GraphCanvas grafocanvas;
    private String nodoInicio, nodoMeta, dataBase;
    private Dijkstra dijkstra;
    private long tiempo;

    @Override
    public Insets insets() {
        return new Insets(10, 10, 10, 10);
    }

    public GraphCanvas getGrafocanvas() {
        return grafocanvas;
    }

    public void setGrafocanvas(GraphCanvas grafocanvas) {
        this.grafocanvas = grafocanvas;
    }
}
